#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "BMCRepository.h"
#include "../ASMUtilities.h"

BMC** BMCRepository;

void BMCRInit(){
    BMCRepository = malloc(sizeof(BMC*) * 0x10000);
    memset(BMCRepository, 0, 0x10000 * sizeof(BMC*));
}

void BMCRRefresh(BMC* Block, uint32_t HardspotLast){
    uint16_t i, FinalAddr, FAInc;
    
    if(!Block)
        return;
    
    if(!(Block->Length))
        return;
    
    FAInc = 0;
    
    if(HardspotLast){
        switch(Block->Instructions[Block->Length-1]->Opcode){
            case 0x6C:
            case 0x4C:
            case 0x20:
                FAInc = 3;
            break;
            
            case 0x40:
            case 0x60:
                FAInc = 1;
            break;
        }
    }
    
    FinalAddr = Block->Instructions[Block->Length-1]->Addr
            + Block->Instructions[Block->Length-1]->Size
            + FAInc;
    
#ifndef ASMUTILITIES
    for(i = Block->StartAddr; i < FinalAddr; i++)
        BMCRepository[i] = Block;
#else
    memset64(((void*)((uint64_t) BMCRepository) + (sizeof(BMC*) * Block->StartAddr)), (uint64_t) Block, FinalAddr - Block->StartAddr);
#endif
}

BMC* BMCRSearch(uint16_t Addr){
    return BMCRepository[Addr];
}

